package anyVehicle;

import javax.vecmath.Vector3f;

import net.minecraft.src.Entity;

import com.bulletphysics.collision.shapes.CollisionShape;
import com.bulletphysics.dynamics.DynamicsWorld;
import com.bulletphysics.dynamics.RigidBody;
import com.bulletphysics.dynamics.RigidBodyConstructionInfo;
import com.bulletphysics.linearmath.MotionState;
import com.bulletphysics.linearmath.Transform;

public class EntityRigidBody extends RigidBody {

	DynamicsWorld world;
	
	public EntityRigidBody(Entity entity, DynamicsWorld world, float mass, MotionState motionState, CollisionShape collisionShape, Vector3f localInertia) {
		super(mass, motionState, collisionShape, localInertia);
		this.world = world;
		this.entity = entity;
	}

	public EntityRigidBody(Entity entity, DynamicsWorld world, float mass, MotionState motionState, CollisionShape collisionShape) {
		super(mass, motionState, collisionShape);
		this.world = world;
		this.entity = entity;
	}

	public EntityRigidBody(Entity entity, DynamicsWorld world, RigidBodyConstructionInfo constructionInfo) {
		super(constructionInfo);
		this.world = world;
		this.entity = entity;
	}

	private Entity entity;

	public Entity getEntity() {
		return entity;
	}

	public void updateRigidBodyFromEntity() {
		world.removeRigidBody(this);
		Transform t = new Transform();
		getCenterOfMassTransform(t);
		t.origin.set((float) entity.posX, (float) entity.posY, (float) entity.posZ);
		setCenterOfMassTransform(t);
		setLinearVelocity(new Vector3f((float) entity.motionX, (float) entity.motionY, (float) entity.motionZ));
		world.addRigidBody(this);
	}

	public void updateEntityFromRigidBody() {
		Transform t = new Transform();
		getCenterOfMassTransform(t);
		entity.setPosition(t.origin.x, t.origin.y, t.origin.z);
		Vector3f v = new Vector3f();
		getLinearVelocity(v);
		entity.setVelocity(v.x, v.y, v.z);

		// System.out.printf("Updated: %s %.5f,%.5f,%.5f\n ", entity,
		// entity.posX, entity.posY, entity.posZ);
		// System.out.println(dynamicsWorld.getNumCollisionObjects());
	}
}
